{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #The Employee That Worked on the Longest Task"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: hardestWorker"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #处理用时最长的那个任务的员工"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>共有 <code>n</code> 位员工，每位员工都有一个从 <code>0</code> 到 <code>n - 1</code> 的唯一 id 。</p>\n",
    "\n",
    "<p>给你一个二维整数数组 <code>logs</code> ，其中 <code>logs[i] = [id<sub>i</sub>, leaveTime<sub>i</sub>]</code> ：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>id<sub>i</sub></code> 是处理第 <code>i</code> 个任务的员工的 id ，且</li>\n",
    "\t<li><code>leaveTime<sub>i</sub></code> 是员工完成第 <code>i</code> 个任务的时刻。所有 <code>leaveTime<sub>i</sub></code> 的值都是 <strong>唯一</strong> 的。</li>\n",
    "</ul>\n",
    "\n",
    "<p>注意，第 <code>i</code> 个任务在第 <code>(i - 1)</code> 个任务结束后立即开始，且第 <code>0</code> 个任务从时刻 <code>0</code> 开始。</p>\n",
    "\n",
    "<p>返回处理用时最长的那个任务的员工的 id 。如果存在两个或多个员工同时满足，则返回几人中 <strong>最小</strong> 的 id 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = 10, logs = [[0,3],[2,5],[0,9],[1,15]]\n",
    "<strong>输出：</strong>1\n",
    "<strong>解释：</strong>\n",
    "任务 0 于时刻 0 开始，且在时刻 3 结束，共计 3 个单位时间。\n",
    "任务 1 于时刻 3 开始，且在时刻 5 结束，共计 2 个单位时间。\n",
    "任务 2 于时刻 5 开始，且在时刻 9 结束，共计 4 个单位时间。\n",
    "任务 3 于时刻 9 开始，且在时刻 15 结束，共计 6 个单位时间。\n",
    "时间最长的任务是任务 3 ，而 id 为 1 的员工是处理此任务的员工，所以返回 1 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = 26, logs = [[1,1],[3,7],[2,12],[7,17]]\n",
    "<strong>输出：</strong>3\n",
    "<strong>解释：</strong>\n",
    "任务 0 于时刻 0 开始，且在时刻 1 结束，共计 1 个单位时间。\n",
    "任务 1 于时刻 1 开始，且在时刻 7 结束，共计 6 个单位时间。\n",
    "任务 2 于时刻 7 开始，且在时刻 12 结束，共计 5 个单位时间。\n",
    "任务 3 于时刻 12 开始，且在时刻 17 结束，共计 5 个单位时间。\n",
    "时间最长的任务是任务 1 ，而 id 为 3 的员工是处理此任务的员工，所以返回 3 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = 2, logs = [[0,10],[1,20]]\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>\n",
    "任务 0 于时刻 0 开始，且在时刻 10 结束，共计 10 个单位时间。\n",
    "任务 1 于时刻 10 开始，且在时刻 20 结束，共计 10 个单位时间。\n",
    "时间最长的任务是任务 0 和 1 ，处理这两个任务的员工的 id 分别是 0 和 1 ，所以返回最小的 0 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 &lt;= n &lt;= 500</code></li>\n",
    "\t<li><code>1 &lt;= logs.length &lt;= 500</code></li>\n",
    "\t<li><code>logs[i].length == 2</code></li>\n",
    "\t<li><code>0 &lt;= id<sub>i</sub> &lt;= n - 1</code></li>\n",
    "\t<li><code>1 &lt;= leaveTime<sub>i</sub> &lt;= 500</code></li>\n",
    "\t<li><code>id<sub>i</sub> != id<sub>i + 1</sub></code></li>\n",
    "\t<li><code>leaveTime<sub>i</sub></code> 按严格递增顺序排列</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [the-employee-that-worked-on-the-longest-task](https://leetcode.cn/problems/the-employee-that-worked-on-the-longest-task/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [the-employee-that-worked-on-the-longest-task](https://leetcode.cn/problems/the-employee-that-worked-on-the-longest-task/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['10\\n[[0,3],[2,5],[0,9],[1,15]]', '26\\n[[1,1],[3,7],[2,12],[7,17]]', '2\\n[[0,10],[1,20]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        x=[]\n",
    "        for i in range(len(logs)):\n",
    "            y=[]\n",
    "            if i==0:\n",
    "                y.append([logs[i][0],logs[i][1]])\n",
    "            else:\n",
    "                y.append([logs[i][0],logs[i][1]-logs[i-1][1]])\n",
    "            x+=y\n",
    "        s=sorted(x,key=lambda i:(-i[1],i[0]))\n",
    "        return s[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        ans = logs[0][0]\n",
    "        curLen = logs[0][1]\n",
    "\n",
    "        for i, [id1, time] in enumerate(logs):\n",
    "            temp = time - logs[i-1][1]\n",
    "            if temp > curLen or (temp == curLen and id1 < ans):\n",
    "                curLen = temp\n",
    "                ans = id1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        res = -1\n",
    "        pre = 0\n",
    "        l = 0\n",
    "        for idx,i in logs:\n",
    "            if i-pre >= l:\n",
    "                if i-pre == l:res = min(res,idx)\n",
    "                else:res = idx\n",
    "                l = i-pre\n",
    "            pre = i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        ans,maxcost=logs[0]\n",
    "        for i in range(1,len(logs)):\n",
    "            id,cost=logs[i][0],logs[i][1]-logs[i-1][1]\n",
    "            if cost>maxcost or (cost==maxcost and id<ans):\n",
    "                ans,maxcost=id,cost\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        ans,ts = logs[0][0],logs[0][1]\n",
    "        for i in range(1,len(logs)):\n",
    "            if logs[i][1] - logs[i-1][1] > ts:\n",
    "                ts = logs[i][1] - logs[i-1][1]\n",
    "                ans = logs[i][0]\n",
    "            elif logs[i][1] - logs[i-1][1] == ts and logs[i][0] < ans:\n",
    "                ans = logs[i][0]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        ans,maxcost=logs[0]\n",
    "        for i in range(1,len(logs)):\n",
    "            idx,cost=logs[i][0],logs[i][1]-logs[i-1][1]\n",
    "            if cost>maxcost or (cost==maxcost and idx<ans):\n",
    "                ans,maxcost=idx,cost\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        ans = logs[0][0]\n",
    "        cnt = logs[0][1]\n",
    "        for i in range(1,len(logs)):\n",
    "            temp = logs[i][1]-logs[i-1][1]\n",
    "            if temp > cnt :\n",
    "                cnt = temp\n",
    "                ans = logs[i][0]\n",
    "            elif temp ==cnt:\n",
    "                ans = min(ans,logs[i][0])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "      max=min=last=0\n",
    "      for id,t in logs:\n",
    "            t=t-last\n",
    "            if max<t or (max==t and id<min):\n",
    "                min,max=id,t\n",
    "            last=t+last\n",
    "      return min"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        minId, minTime = logs[0]\n",
    "        endTime = logs[0][1]\n",
    "        for i,t in logs:\n",
    "            if t-endTime > minTime: minId,minTime = i, t-endTime\n",
    "            elif t-endTime == minTime: minId = min(i, minId)\n",
    "            endTime = t\n",
    "        return minId"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        key = -1\n",
    "        cot = 0\n",
    "        for x in logs:\n",
    "            max_s = x[1]\n",
    "            x[1] = x[1]-cot\n",
    "            if key < x[1]:\n",
    "                key = x[1]\n",
    "            cot = max_s\n",
    "        lt = []\n",
    "        for x,y in logs:\n",
    "            if y == key:\n",
    "                lt.append(x)\n",
    "        lt.sort()\n",
    "        return lt[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        last_time = longest_time = logs[0][1]\n",
    "        longest_id = logs[0][0]\n",
    "        for id, time in logs[1:]:\n",
    "            used_time = time - last_time\n",
    "            last_time = time\n",
    "            if used_time > longest_time or used_time == longest_time and id < longest_id:\n",
    "                longest_time = used_time\n",
    "                longest_id = id\n",
    "        return longest_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        label=[0 for i in range(n)]\n",
    "        label[logs[0][0]]=logs[0][1]\n",
    "        for i in range(1, len(logs)):\n",
    "            label[logs[i][0]]=max(logs[i][1]-logs[i-1][1], label[logs[i][0]])\n",
    "        result=0\n",
    "        op=n\n",
    "        for i in range(n):\n",
    "            if label[i]>result:\n",
    "                result=label[i]\n",
    "                op=i\n",
    "        return op"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        ans, maxcost = logs[0]\n",
    "        for i in range(1, len(logs)):\n",
    "            idx, cost = logs[i][0], logs[i][1] - logs[i - 1][1]\n",
    "            if cost > maxcost or (cost == maxcost and idx < ans):\n",
    "                ans, maxcost = idx, cost\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        ans, max_t = logs[0]\n",
    "        for (_, t1), (i, t) in pairwise(logs):\n",
    "            t -= t1\n",
    "            if t > max_t or t == max_t and i < ans:\n",
    "                ans, max_t = i, t\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        ans, max_t = logs[0]\n",
    "        for (_, t1), (i, t) in pairwise(logs):\n",
    "            t -= t1\n",
    "            if t > max_t or t == max_t and i < ans:\n",
    "                ans, max_t = i, t\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        last = mx = ans = 0\n",
    "        for uid, t in logs:\n",
    "            t -= last\n",
    "            if mx < t or (mx == t and ans > uid):\n",
    "                ans, mx = uid, t\n",
    "            last += t\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        ans=logs[0][0]\n",
    "        maxcost=logs[0][1]\n",
    "        for i in range(1,len(logs)):\n",
    "            idx=logs[i][0]\n",
    "            cost=logs[i][1]-logs[i-1][1]\n",
    "            if cost>maxcost or (cost==maxcost and idx<ans):\n",
    "               ans=idx\n",
    "               maxcost=cost\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        last = mx = ans = 0\n",
    "        for uid, t in logs:\n",
    "            t -= last\n",
    "            if mx < t or (mx == t and ans > uid):\n",
    "                ans, mx = uid, t\n",
    "            last += t\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        tmp = 0\n",
    "        maxnum = 0\n",
    "        for a,b in logs:\n",
    "            if maxnum < b - tmp:\n",
    "                maxnum = b - tmp\n",
    "                idx = a\n",
    "            if maxnum == b - tmp:\n",
    "                idx = min(idx,a)\n",
    "            tmp = b    \n",
    "\n",
    "        return idx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        longestTime = 0\n",
    "        start = 0\n",
    "        res = logs[0][0]\n",
    "        for id, leaveTime in logs:\n",
    "            cost = leaveTime - start\n",
    "            start = leaveTime\n",
    "            if cost > longestTime or (cost == longestTime and id < res):\n",
    "                longestTime = cost\n",
    "                res = id\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        pre = 0\n",
    "        res = 0\n",
    "        max_ = 0\n",
    "        for i,t in logs:\n",
    "            if t - pre > max_:\n",
    "                max_ = t - pre\n",
    "                res = i\n",
    "            elif t - pre == max_:\n",
    "                res = min(res,i)\n",
    "            pre = t\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        time, res_id, old = 0, 0, 0\n",
    "        for li in logs:\n",
    "            if li[1] - old > time:\n",
    "                time = li[1] - old\n",
    "                res_id = li[0]\n",
    "            elif li[1] - old == time:\n",
    "                if li[0] < res_id:\n",
    "                    res_id = li[0]\n",
    "            old = li[1]\n",
    "        return res_id\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "\n",
    "        # 计算处理时长\n",
    "        max_id, max_time = logs[0][0] , logs[0][1]\n",
    "        for i in range(1, len(logs)):\n",
    "            itime = logs[i][1] - logs[i-1][1]\n",
    "            if itime > max_time:\n",
    "                max_time = itime\n",
    "                max_id = logs[i][0]\n",
    "            elif itime == max_time:\n",
    "                if logs[i][0] < max_id:\n",
    "                    max_id = logs[i][0]\n",
    "\n",
    "        return max_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        result = 0\n",
    "        maxtime = 0\n",
    "        lasttime = 0\n",
    "        for i in range(len(logs)):\n",
    "            temp = logs[i]\n",
    "            time = temp[1] - lasttime\n",
    "            lasttime = temp[1]\n",
    "            if time > maxtime:\n",
    "                maxtime = time\n",
    "                result = temp[0]\n",
    "            elif time == maxtime:\n",
    "                result = result if result < temp[0] else temp[0]\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        ans, maxcost = logs[0]\n",
    "        for i in range(1, len(logs)):\n",
    "            idx, cost = logs[i][0], logs[i][1] - logs[i - 1][1]\n",
    "            if cost > maxcost or (cost == maxcost and idx < ans):\n",
    "                ans, maxcost = idx, cost\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        maxtime=0\n",
    "        lastime=0\n",
    "        for i in logs:\n",
    "            if i[1]-lastime>maxtime:\n",
    "                result=i[0]\n",
    "                maxtime=i[1]-lastime\n",
    "            elif i[1]-lastime==maxtime:\n",
    "                result=min(result,i[0])\n",
    "            lastime=i[1]\n",
    "            \n",
    "        return result\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        longest = logs[0][1] \n",
    "        n = len(logs)\n",
    "        hardest = logs[0][0]\n",
    "        for i in range (1,n) :\n",
    "            if logs[i][1] - logs[i-1][1] > longest :\n",
    "                longest = logs[i][1] - logs[i-1][1] \n",
    "                hardest = logs[i][0]\n",
    "            elif logs[i][1] - logs[i-1][1] == longest  and hardest > logs[i][0] :\n",
    "                hardest = logs[i][0] \n",
    "        return hardest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        ans = logs[0][0]\n",
    "        max_leave = logs[0][1]\n",
    "        for index, val in enumerate(logs):\n",
    "            if index != 0:\n",
    "                leave = val[1] - logs[index - 1][1]\n",
    "                if leave > max_leave:\n",
    "                    ans = val[0]\n",
    "                    max_leave = leave\n",
    "                elif leave == max_leave:\n",
    "                    ans = min(ans, val[0])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        m, ans = 0, -1\n",
    "        logs = [[-1, 0]] + logs\n",
    "        for i in range(len(logs) - 1, 0, -1):\n",
    "            p, t = logs[i][0], logs[i][1] - logs[i - 1][1]\n",
    "            if t > m:\n",
    "                m = t\n",
    "                ans = p\n",
    "            elif t == m:\n",
    "                ans = min(ans, p)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    #n:员工总数，logs:记录数组，每一项为[id,工作时间]\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        id=logs[0][0]\n",
    "        max=logs[0][1] #将初始员工的编号和工作时间设为logs数组的第一项\n",
    "        for i in range(1,len(logs)): #遍历logs数组，从第二项开始\n",
    "            ids=logs[i][0] \n",
    "            cost=logs[i][1]-logs[i-1][1] #比较当前工作时间差和之前的最大工作时间\n",
    "            if(max<cost or (ids<id and cost>=max)): #如果当前员工的工作时间更长，则更新最大工作时间和员工编号\n",
    "                id,max=ids,cost\n",
    "        return id \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        ans,max_val = logs[0]\n",
    "        for (_,t1),(i,t2) in pairwise(logs):\n",
    "            t = t2-t1\n",
    "            if t > max_val: ans,max_val = i,t2-t1\n",
    "            if t == max_val: ans = min(ans,i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        ans, maxcost = logs[0]\n",
    "        for i in range(1, len(logs)):\n",
    "            idx, cost = logs[i][0], logs[i][1] - logs[i - 1][1]\n",
    "            if cost > maxcost or (cost == maxcost and idx < ans):\n",
    "                ans, maxcost = idx, cost\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        tmp = []\n",
    "        tmp2=[]\n",
    "\n",
    "        for i in range(len(logs)):\n",
    "            if i == 0:\n",
    "                ans = logs[i][1]\n",
    "            else:\n",
    "                ans = logs[i][1] - logs[i - 1][1]\n",
    "            tmp.append(ans)\n",
    "        maxx=max(tmp)\n",
    "        for i in range(len(tmp)):\n",
    "            if tmp[i]==maxx:\n",
    "                tmp2.append(logs[i][0])\n",
    "        return min(tmp2)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        ans = logs[0][0]\n",
    "        mxtime = logs[0][1]\n",
    "        n = len(logs)\n",
    "        for i in range(1,n):\n",
    "            if logs[i][1] - logs[i-1][1] > mxtime:\n",
    "                mxtime = logs[i][1] - logs[i-1][1]\n",
    "                ans = logs[i][0]\n",
    "            elif logs[i][1] - logs[i-1][1] == mxtime:\n",
    "                ans = min(ans, logs[i][0])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        worktime = [0] * n\n",
    "        worktime[logs[0][0]] = logs[0][1]\n",
    "        for i in range(1, len(logs)):\n",
    "            if logs[i][1] - logs[i-1][1] > worktime[logs[i][0]]:\n",
    "                worktime[logs[i][0]] = logs[i][1] - logs[i-1][1]\n",
    "        \n",
    "        return worktime.index(max(worktime))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        j = 1\n",
    "        max_time = logs[0][1]\n",
    "        return_id = logs[0][0]\n",
    "        while j < len(logs):\n",
    "            if logs[j][1] - logs[j-1][1] > max_time:\n",
    "                max_time = logs[j][1] - logs[j-1][1]\n",
    "                return_id = logs[j][0]\n",
    "            elif logs[j][1] - logs[j-1][1] ==  max_time:\n",
    "                return_id = min(logs[j][0],return_id)\n",
    "            j += 1\n",
    "        return return_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        pre = logs[0][1] \n",
    "        n = len(logs)\n",
    "        hardest = logs[0][0]\n",
    "        for i in range (1,n) :\n",
    "            if logs[i][1] - logs[i-1][1] > pre :\n",
    "                pre = logs[i][1] - logs[i-1][1] \n",
    "                hardest = logs[i][0]\n",
    "            elif logs[i][1] - logs[i-1][1] == pre  and hardest > logs[i][0] :\n",
    "                hardest = logs[i][0] \n",
    "        return hardest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        ans,ts = logs[0][0],logs[0][1]\n",
    "        for i in range(1,len(logs)):\n",
    "            if logs[i][1] - logs[i-1][1] > ts:\n",
    "                ts = logs[i][1] - logs[i-1][1]\n",
    "                ans = logs[i][0]\n",
    "            elif logs[i][1] - logs[i-1][1] == ts and logs[i][0] < ans:\n",
    "                ans = logs[i][0]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        for i in range(len(logs) - 1, 0, -1):\n",
    "            logs[i][1] = logs[i][1] - logs[i - 1][1]\n",
    "        return sorted(logs, key=lambda x: (x[1], -x[0]))[-1][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        max_time, end_time, id = 0, 0, logs[0][0]\n",
    "        for item in logs:\n",
    "            use_time = item[1] - end_time\n",
    "            if use_time > max_time:\n",
    "                max_time = use_time\n",
    "                id = item[0]\n",
    "            elif use_time == max_time and id > item[0]:\n",
    "                id = item[0]\n",
    "            end_time = item[1]\n",
    "        return id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        length = len(logs)\n",
    "        max = logs[0][1]\n",
    "        ans = logs[0][0]\n",
    "        for i in range(1, length):\n",
    "            if logs[i][1] - logs[i - 1][1] > max:\n",
    "                max = logs[i][1] - logs[i - 1][1]\n",
    "                ans = logs[i][0]\n",
    "            if logs[i][1] - logs[i - 1][1] == max:\n",
    "                if logs[i][0] < ans:\n",
    "                    ans = logs[i][0]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        ans, max_interval = logs[0]\n",
    "        for i in range(len(logs)-1):\n",
    "            interval = logs[i+1][1] - logs[i][1]\n",
    "            if interval > max_interval:\n",
    "                ans, max_interval = logs[i+1][0], interval\n",
    "            if interval == max_interval:\n",
    "                ans = min(ans, logs[i+1][0])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        max_time = 0\n",
    "        max_id = -1\n",
    "        logs.sort(key=lambda x:x[1])\n",
    "        pre = 0\n",
    "        for id, leave_time in logs:\n",
    "            time = leave_time - pre\n",
    "            if time > max_time:\n",
    "                max_time = time\n",
    "                max_id = id\n",
    "            elif time == max_time and id < max_id:\n",
    "                max_id = id\n",
    "            pre = leave_time\n",
    "        return max_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        ans=logs[0][0]\n",
    "        maxcost=logs[0][1]\n",
    "        for i in range(1,len(logs)):\n",
    "            idx=logs[i][0]\n",
    "            cost=logs[i][1]-logs[i-1][1]\n",
    "            if cost>maxcost or (cost==maxcost and idx<ans):\n",
    "               ans=idx\n",
    "               maxcost=cost\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        longestId = logs[0][0]\n",
    "        longestTime = logs[0][1]\n",
    "        for i in range(1, len(logs), 1):\n",
    "            tempId = logs[i][0]\n",
    "            tempTime = logs[i][1] - logs[i - 1][1]\n",
    "            if longestTime == tempTime:\n",
    "                if longestId > tempId:\n",
    "                    longestId = tempId\n",
    "            elif longestTime < tempTime:\n",
    "                longestId = tempId\n",
    "                longestTime = tempTime\n",
    "        return longestId\n",
    "            \n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        long_time=logs[0][1]\n",
    "        long_time_ls=[logs[0][0]]\n",
    "        for i in range(1,len(logs)):\n",
    "            if logs[i][1]-logs[i-1][1]>long_time:\n",
    "                long_time=logs[i][1]-logs[i-1][1]\n",
    "                long_time_ls=[logs[i][0]]\n",
    "            elif logs[i][1]-logs[i-1][1]==long_time:\n",
    "                long_time_ls.append(logs[i][0])\n",
    "        return min(long_time_ls)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        ans = [logs[0][0]]\n",
    "        lis0 = [logs[0][1]]\n",
    "        for i in range(0, len(logs)-1):\n",
    "            a = int(logs[i+1][1] - logs[i][1])\n",
    "            if a > lis0[0]:\n",
    "                ans = [logs[i+1][0]]\n",
    "                lis0 = [a]\n",
    "            elif a == lis0[0]:\n",
    "                ans.append(logs[i+1][0])\n",
    "        return min(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        ans = [0,0]\n",
    "        last = 0\n",
    "        for log in logs:\n",
    "            if log[1]-last > ans[1]:\n",
    "                ans[1] = log[1]-last\n",
    "                ans[0] = log[0]\n",
    "            elif log[1]-last == ans[1]:\n",
    "                ans[0] = min(ans[0],log[0])\n",
    "            last = log[1]\n",
    "        return ans[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        start = 0\n",
    "        result = 0\n",
    "        longest = 0\n",
    "        for job in range(0,len(logs)):\n",
    "            joblen = logs[job][1] - start\n",
    "            if joblen > longest:\n",
    "                result = logs[job][0]\n",
    "                longest = joblen\n",
    "            elif joblen == longest:\n",
    "                result = min(result,logs[job][0])\n",
    "            start = logs[job][1]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        e_id = -1\n",
    "        max_time = 0\n",
    "        last_time = 0\n",
    "        for a, b in logs:\n",
    "            work_time = b - last_time\n",
    "            if work_time > max_time or (work_time == max_time and a < e_id):\n",
    "                max_time = work_time\n",
    "                e_id = a\n",
    "            last_time = b\n",
    "        return e_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        re=logs[0][0]\n",
    "        maxt=logs[0][1]\n",
    "        for i in range(len(logs)):\n",
    "            if logs[i][1]-logs[i-1][1]>maxt:\n",
    "                maxt=logs[i][1]-logs[i-1][1]\n",
    "                re=logs[i][0]\n",
    "            elif logs[i][1]-logs[i-1][1]==maxt:\n",
    "                re=min(re,logs[i][0])\n",
    "        return re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        last_time = longest_time = logs[0][1]\n",
    "        longest_id = logs[0][0]\n",
    "        for id, time in logs[1:]:\n",
    "            used_time = time - last_time\n",
    "            last_time = time\n",
    "            if used_time > longest_time or used_time == longest_time and id < longest_id:\n",
    "                longest_time = used_time\n",
    "                longest_id = id\n",
    "        return longest_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        length = logs.__len__()\n",
    "        for i in range(length-1, 0, -1):\n",
    "            logs[i][1] = logs[i][1] - logs[i-1][1]\n",
    "        logs.sort(key=lambda row: row[1])\n",
    "        maximum = logs[-1][1]\n",
    "        return min([val[0] for val in logs if val[1] == maximum])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        min_id, max_time = n, 0\n",
    "        for index in range(len(logs)-1, -1, -1):\n",
    "            if index != 0:\n",
    "                logs[index] = [logs[index][0], logs[index][1]-logs[index-1][1]]\n",
    "            if logs[index][1] > max_time:\n",
    "                 max_time = logs[index][1]\n",
    "                 min_id = logs[index][0]\n",
    "            if logs[index][1] == max_time and logs[index][0] < min_id:\n",
    "                min_id = logs[index][0]\n",
    "        # ids = [id for id, cost_time in logs if cost_time == max_time]\n",
    "        return min_id\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        lasttime = logs[0][1]\n",
    "        ans = logs[0][0]\n",
    "        for i in range(1,len(logs)):\n",
    "            if lasttime<logs[i][1]-logs[i-1][1]:\n",
    "                lasttime = logs[i][1]-logs[i-1][1]\n",
    "                ans = logs[i][0]\n",
    "            elif lasttime==logs[i][1]-logs[i-1][1]:\n",
    "                ans = min(ans,logs[i][0])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        time=logs[0][1]\n",
    "        op=logs[0][0]\n",
    "        n=len(logs)\n",
    "        for i in range(1, n):\n",
    "            label=logs[i][1]-logs[i-1][1]\n",
    "            if label>time:\n",
    "                time=logs[i][1]-logs[i-1][1]\n",
    "                op=logs[i][0]\n",
    "            elif label==time:\n",
    "                if op>logs[i][0]:\n",
    "                    op=logs[i][0]\n",
    "        return op"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        # max = logs[0][1]\n",
    "        # lst = []\n",
    "        # lst.append(logs[0][0])\n",
    "        # for i in range(1, len(logs)):\n",
    "        #     ans = logs[i][1]-logs[i-1][1]\n",
    "        #     if ans>max:\n",
    "        #         max = ans\n",
    "        #         lst = list()\n",
    "        #         lst.append(logs[i][0])\n",
    "        #     elif ans == max:\n",
    "        #         lst.append(logs[i][0])\n",
    "\n",
    "        ans, maxcost = logs[0]\n",
    "        for i in range(1,len(logs)):\n",
    "            idx, cost = logs[i][0], logs[i][1]-logs[i-1][1]\n",
    "            if cost>maxcost or (cost==maxcost and idx<ans):\n",
    "                ans, maxcost = idx, cost\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        cnt = [0] * n\n",
    "        pre = 0\n",
    "        for id, t in logs:\n",
    "            cnt[id] = max(cnt[id], t - pre)\n",
    "            pre = t\n",
    "\n",
    "        return cnt.index(max(cnt))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        ans, maxcost = logs[0]\n",
    "        for i in range(1, len(logs)):\n",
    "            idx, cost = logs[i][0], logs[i][1] - logs[i - 1][1]\n",
    "            if cost > maxcost or (cost == maxcost and idx < ans):\n",
    "                ans, maxcost = idx, cost\n",
    "        return ans\n",
    "\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        time=logs[0][1]\n",
    "        op=logs[0][0]\n",
    "        n=len(logs)\n",
    "        for i in range(1, n):\n",
    "            label=logs[i][1]-logs[i-1][1]\n",
    "            if label>time:\n",
    "                time=logs[i][1]-logs[i-1][1]\n",
    "                op=logs[i][0]\n",
    "            elif label==time:\n",
    "                if op>logs[i][0]:\n",
    "                    op=logs[i][0]\n",
    "        return op"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        # 找出单个任务耗时最长的任务,\n",
    "        # 然后记录这个任务的员工编号.\n",
    "        # 将这个员工的编号和最长耗时入栈. \n",
    "        # 如果之前有数据,就比较耗时,\n",
    "        # 如果大于已有耗时,就弹出已有\n",
    "        # 如果等于就比较id.记录较小的一个\n",
    "        # 如果小于就不添加\n",
    "\n",
    "        lastTime=0  # 上一个任务完成时刻,用于计算下一个任务耗时\n",
    "        maxTime = 0  # 长度2的数组,0位置:任务耗时,1位置:员工编号 \n",
    "        maxWId = n\n",
    "        for task in logs:\n",
    "            time = task[1] - lastTime\n",
    "            lastTime =  task[1]\n",
    "            if time > maxTime:\n",
    "                maxTime = time\n",
    "                maxWId = task[0]\n",
    "            elif time == maxTime and task[0] < maxWId:             \n",
    "                maxWId = task[0]\n",
    "        return maxWId"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        m = len(logs)\n",
    "        id = -1\n",
    "        t = -1\n",
    "        pre_t = 0\n",
    "        for idx, leave_t in logs:\n",
    "            if leave_t - pre_t > t:\n",
    "                t = leave_t - pre_t\n",
    "                id = idx\n",
    "            elif leave_t - pre_t == t:\n",
    "                id = min(id, idx)\n",
    "            pre_t = leave_t\n",
    "        \n",
    "        return id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        ans, maxcost = logs[0]\n",
    "        for i in range(1, len(logs)):\n",
    "            idx, cost = logs[i][0], logs[i][1] - logs[i - 1][1]\n",
    "            if cost > maxcost or (cost == maxcost and idx < ans):\n",
    "                ans, maxcost = idx, cost\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        lognote = 0\n",
    "        timerecord = 0\n",
    "        res = 0\n",
    "        for log in logs:\n",
    "            time = log[1] - lognote\n",
    "            lognote = log[1]\n",
    "            if time > timerecord:\n",
    "                res = log[0]\n",
    "                timerecord = time\n",
    "            if time == timerecord and log[0] < res:\n",
    "                res = log[0]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        last = 0\n",
    "        a = [0] * n\n",
    "        max_val = 0\n",
    "        res_id = 0\n",
    "        for i, t in logs:\n",
    "            a[i] += t - last\n",
    "            if t - last > max_val:\n",
    "                max_val = t - last\n",
    "                res_id = i\n",
    "            elif t - last == max_val:\n",
    "                res_id = min(i, res_id)\n",
    "            last = t\n",
    "        # print(a)\n",
    "        return res_id\n",
    "        \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        hash = defaultdict(int)\n",
    "        start = 0\n",
    "        for log in logs:\n",
    "            hash[log[0]] = max(log[1] - start, hash[log[0]])\n",
    "            start = log[1]\n",
    "        max_time, idx = 0, 0\n",
    "        for key in hash:\n",
    "            if max_time < hash[key]:\n",
    "                max_time = hash[key]\n",
    "                idx = key\n",
    "            elif max_time == hash[key]:\n",
    "                idx = min(idx, key)\n",
    "        return idx\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        pre = 0\n",
    "        res = defaultdict(int)\n",
    "        for i in range(len(logs)):\n",
    "            res[logs[i][0]]=max(res[logs[i][0]],logs[i][1]-pre)\n",
    "            pre = logs[i][1]\n",
    "        print(res)\n",
    "        return sorted(res.items(),key = lambda x:(-x[1],x[0]))[0][0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        n=len(logs)\n",
    "        ans,m,lasttime=0,-1,0\n",
    "        for log in logs:\n",
    "            if log[1]-lasttime>m:\n",
    "                m=log[1]-lasttime\n",
    "                ans=log[0]\n",
    "            elif log[1]-lasttime==m:\n",
    "                ans=min(ans,log[0])\n",
    "            lasttime=log[1]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        ans, maxcost = logs[0]\n",
    "        for i in range(1, len(logs)):\n",
    "            idx, cost = logs[i][0], logs[i][1] - logs[i - 1][1]\n",
    "            if cost > maxcost or (cost == maxcost and idx < ans):\n",
    "                ans, maxcost = idx, cost\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hardestWorker(self, n: int, logs: List[List[int]]) -> int:\n",
    "        max_id = max_time = -1\n",
    "        prev_time = 0\n",
    "        for uid,leave_time in logs:\n",
    "            use_time = leave_time - prev_time\n",
    "            prev_time = leave_time\n",
    "            if use_time > max_time:\n",
    "                print(use_time, max_time)\n",
    "                max_id = uid\n",
    "                max_time = use_time\n",
    "            elif use_time == max_time and uid < max_id:\n",
    "                max_id = uid\n",
    "        return max_id"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
